import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:xiongan_app_yy/pages/common/LoadBuilder.dart';
import 'package:xiongan_app_yy/provider/automounting_provider.dart';
import 'package:xiongan_app_yy/widgets/comm/left_icon_widget.dart';

//自动上架——详情列表
class AutoMountingDetailList extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _AutoMountingDetailList();
  }
}

//ListWidget
class _AutoMountingDetailList extends State<AutoMountingDetailList> {

  List<dynamic> _data = List();

  String nameStr = "";

  @override
  Widget build(BuildContext context) {
    nameStr = Provider.of<AutoMountingProvider>(context).inPutawaytypename;
    for(int i=0;i<Provider.of<AutoMountingProvider>(context).detailListData.length;i++){
      if(Provider.of<AutoMountingProvider>(context).detailListData[i]["inPutawaytypename"]==nameStr){
        _data=Provider.of<AutoMountingProvider>(context).detailListData[i]["inautoputawayList"];
      }
    }

    return Scaffold(
      appBar: AppBar(
        leading: LeftIconWidget(icon: Icons.chevron_left, isize: 36),
        backgroundColor: Color(0XFF00A5F1),
        title: Text(
          "自动上架详情列表",
          style: TextStyle(fontSize: 18),
        ),
        elevation: 0,
        centerTitle: true,
      ),
      body: LoadBuilder(
        visibility: Provider.of<AutoMountingProvider>(context).netStatus2,
        builder: Scrollbar(
          child:RefreshIndicator(
    child: ListView.separated(
            itemBuilder: (BuildContext context, int index) {
              return TextCartItem(
                data: _data,
                index: index,
                onTapFuns: (int index) {
                  Provider.of<AutoMountingProvider>(context).setDetailData(obj:_data[index]);
                  print("选中的数据详情~~~~~~~~~~~~~~");
                  print(_data[index]);
                  print(_data[index]['isputaway']);
                  print(_data[index]['issendputawaytask']); //false可以上架
                  print(_data[index]['issendputdowntask']); //false可以下架                print(_data[index]);
                  Navigator.of(context).pushNamed("/automounting_ddetail");
                },
              );
            },
            separatorBuilder: (BuildContext context, int index) {
              return Divider(color: Colors.black26);
            },
            itemCount: _data.length),onRefresh: _refresh,
          )
        ),
      )
    );
  }

  Future<void> _refresh() async {
    await Future.delayed(Duration(seconds: 1), () {
      return null;
    });
    Provider.of<AutoMountingProvider>(context).getDetailListData(orderid: Provider.of<AutoMountingProvider>(context).orderId);
  }
}

class _Text extends Text {
  TextStyle style;
  _Text(String data,
      {this.style = const TextStyle(color: Color(0xff646464), fontSize: 14)})
      : super(data, style: style);
}

class TextCartItem extends StatelessWidget {
  final List<dynamic> data;
  final int index;
  final Function(int index) onTapFuns;

  TextCartItem(
      {Key key, @required this.data, @required this.index, this.onTapFuns})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return GestureDetector(
      child: Container(
          padding: EdgeInsets.all(10.0),
          decoration: BoxDecoration(
              //解决GestureDetector Widget事件触发面问题
              border: Border.all(color: Colors.transparent, width: 1)),
          child: Column(
            children: <Widget>[
              Align(
                alignment: Alignment.bottomLeft,
                child: _Text("物资名称:${data[index]['materialname']}"),
              ),
              Container(
                padding: EdgeInsets.only(top: 5, bottom: 5),
                child: Align(
                  alignment: Alignment.bottomLeft,
                  child: _Text("上架库区:${data[index]['areaname']}"),
                ),
              ),
              Container(
                padding: EdgeInsets.only(top: 5, bottom: 5),
                child: Align(
                  alignment: Alignment.bottomLeft,
                  child: _Text("上架货架:${data[index]['shelfname']}"),
                ),
              ),
              Container(
                padding: EdgeInsets.only(top: 5),
                child: Align(
                  alignment: Alignment.bottomLeft,
                  child: _Text("上架货位:${data[index]['tolocationcode']}"),
                ),
              ),
            ],
          )),
      onTap: () {
        if (onTapFuns != null) {
          onTapFuns(index);
        }
      },
    );
  }
}
